package com.robot.admin.controller;

import com.alibaba.fastjson.JSON;
import com.robot.admin.client.MemberClient;
import com.robot.admin.pojo.client.Feedback;
import com.robot.admin.service.AdminService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 反馈模块
 *
 * @author lin
 */
@RestController
@CrossOrigin
@RequestMapping("/feedback")
public class FeedbackController {
    @Autowired
    private AdminService adminService;

    @Autowired
    private MemberClient memberClient;

    @Autowired
    private HttpServletRequest request;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());


    /**
     * 所有反馈并分页
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "{page}/{size}", produces = "application/json; charset=utf-8")
    public Result findFeedbacksByIdAndMemberId(@PathVariable int page, @PathVariable int size) {
        Claims claims = (Claims) request.getAttribute("robot_admin");
        if (claims == null) {
            return new Result(StatusCode.ERROR_TOKEN_EXPIRED);
        }
        logger.info("claims.getId()=" + claims.getId());
        //前端调用findByToken接口检查过了,所以这里不再对管理员是否存在和状态进行判断

        return new Result(StatusCode.SUCCESS_QUERY, JSON.parseObject(memberClient.thirdFindFeedbacksByDeletedAtNullOrderByCreatedAt(page, size)));
    }

    /**
     * 某一反馈详情
     *
     * @param id
     * @return
     */
    @GetMapping(value = "{id}", produces = "application/json; charset=utf-8")
    public Result findFeedbackById(@PathVariable String id) {
        Claims claims = (Claims) request.getAttribute("robot_admin");
        if (claims == null) {
            return new Result(StatusCode.ERROR_TOKEN_EXPIRED);
        }
        logger.info("claims.getId()=" + claims.getId() + ",feedback's id=" + id);
        //前端调用findByToken接口检查过了,所以这里不再对管理员是否存在和状态进行判断

        Feedback feedbackInDb = JSON.parseObject(memberClient.thirdFindFeedbackById(id), Feedback.class);
        logger.info("feedbackInDb=" + feedbackInDb);
        String message = checkPojo(feedbackInDb);
        if (!"".equals(message)) {
            return new Result(StatusCode.ERROR, message);
        }

        return new Result(StatusCode.SUCCESS_QUERY, feedbackInDb);
    }

    /**
     * 审核反馈
     *
     * @param feedback
     * @param id
     * @return
     */
    @RequestMapping(value = "{id}", method = RequestMethod.PUT, produces = "application/json; charset=utf-8")
    public Result updateFeedbackById(@RequestBody Feedback feedback, @PathVariable String id) {
        Claims claims = (Claims) request.getAttribute("robot_admin");
        if (claims == null) {
            return new Result(StatusCode.ERROR_TOKEN_EXPIRED);
        }
        logger.info("claims.getId()=" + claims.getId());
        //前端调用findByToken接口检查过了,所以这里不再对管理员是否存在和状态进行判断

        Feedback feedbackInDb = JSON.parseObject(memberClient.thirdFindFeedbackById(id), Feedback.class);
        logger.info("feedbackInDb=" + feedbackInDb);
        String message = checkPojo(feedbackInDb);
        if (!"".equals(message)) {
            return new Result(StatusCode.ERROR, message);
        }

        String status = feedback.getStatus();
        if (status == null || "".equals(status)) {
            return new Result(StatusCode.ERROR, "状态必填");
        }
        /**
         * feedback表status 状态 0-待处理 1-处理中 2-处理成功
         */
        String feedBackStatusOne = "1";
        String feedBackStatusTwo = "2";
        if (feedBackStatusOne.equals(status)) {

        } else if (feedBackStatusTwo.equals(status)) {
            if (feedback.getRemark() == null || "".equals(feedback.getRemark())) {
                return new Result(StatusCode.ERROR, "请填写反馈处理结果");
            }
        } else {
            return new Result(StatusCode.ERROR, "状态错误");
        }
        if (feedbackInDb.getDeletedAt() != null) {
            return new Result(StatusCode.ERROR, "该条反馈已被删除");
        }
        feedbackInDb.setRemark(feedback.getRemark());
        feedbackInDb.setStatus(status);
        memberClient.thirdUpdateFeedback(JSON.toJSONString(feedbackInDb));
        return new Result(StatusCode.SUCCESS_UPDATED);
    }

    /**
     * 删除某一反馈详情
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "{id}", produces = "application/json; charset=utf-8")
    public Result updateFeedbackDeletedAtNullById(@PathVariable String id) {
        Claims claims = (Claims) request.getAttribute("robot_admin");
        if (claims == null) {
            return new Result(StatusCode.ERROR_TOKEN_EXPIRED);
        }
        logger.info("claims.getId()=" + claims.getId());
        //前端调用findByToken接口检查过了,所以这里不再对管理员是否存在和状态进行判断

        memberClient.thirdUpdateFeedbackDeletedAtNullById(id);
        return new Result(StatusCode.SUCCESS_DELETED);
    }

    /**
     * 检查实体类是否存在
     *
     * @param feedbackInDb
     * @return
     */
    public String checkPojo(Feedback feedbackInDb) {
        if (feedbackInDb == null) {
            return ("该条反馈不存在");
        }
        if (feedbackInDb.getDeletedAt() != null) {
            return ("该条反馈已被删除");
        }
        return "";
    }
}

